package com.ITArmy.service.impl;

import com.ITArmy.beans.Configs;
import com.ITArmy.beans.User;
import com.ITArmy.common.ServerResponse;
import com.ITArmy.dao.ConfigsMapper;
import com.ITArmy.dao.UserMapper;
import com.ITArmy.service.ConfigService;
import com.ITArmy.util.GetUserSession;
import com.ITArmy.util.UploadPicture;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import sun.security.krb5.Config;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class ConfigServiceImpl implements ConfigService {
    //获取全部的主题
    @Autowired
    ConfigsMapper configsMapper;
    @Autowired
    UserMapper userMapper;

    GetUserSession getUserSession = new GetUserSession();

    @Override
    public List<Configs> getAllConfig() {
        return configsMapper.getAllConfig();
    }

    //用户修改自己的主题

    @Override
    public ServerResponse changeTheme(int cId, HttpServletRequest request) {
        User user = getUserSession.getUser(request);
        user.setCoId(cId);
        int resultCount = userMapper.updateByPrimaryKeySelective(user);
        if (resultCount > 0) {
            //更换session中的主题信息
            HttpSession session = request.getSession();
            session.setAttribute("config", configsMapper.selectByPrimaryKey(cId));
            return ServerResponse.createBySuccess("修改成功...");
        } else {
            return ServerResponse.createByErrorMesage("修改失败...");
        }

    }


    @Override
    public ServerResponse uploadIndexPicture(int configId, int location, MultipartFile picture, HttpServletRequest request) {
        UploadPicture uploadPicture = new UploadPicture();
        String path = "/static/uploads/indexPicture";
        String realPath = (String) uploadPicture.upload(picture, request, path).getData();
        if (realPath != null) {
            //将地址信息上传到数据库
            Configs configs = new Configs();
            configs.setcId(configId);
            if (location == 1) {
                configs.setIndexP1(realPath);
            } else if (location == 2) {
                configs.setIndexP2(realPath);
            } else if (location == 3) {
                configs.setIndexP3(realPath);
            }else{
                return ServerResponse.createBySuccessMessage("上传失败...");
            }
            return ServerResponse.createBySuccessMessage("上传成功...");

        } else {
            return ServerResponse.createBySuccessMessage("上传失败...");
        }

    }
}
